package friot.service.cloud.feign;

import friot.feign.clients.IDeviceClient;
import friot.model.iot.entity.IotThings;
import friot.model.iot.vo.IotDeviceVo;
import friot.service.cloud.service.IotDeviceService;
import friot.service.cloud.service.IotThingsService;
import friot.utils.tools.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author He Kuntao
 * @desc Feign Client
 * @date 2024/3/14 22:47
 */
@RestController
@RequestMapping("/api/iot/device")
public class DeviceClient implements IDeviceClient {

    @Resource
    private IotDeviceService iotDeviceService;
    @Resource
    private IotThingsService iotThingsService;

    /**
     * 根据用户ID查询设备
     *
     * @param id 用户ID
     * @return 设备集合
     */
    @Override
    @GetMapping("/{id}")
    public Result listDeviceByUserId(@PathVariable String id) {
        List<IotDeviceVo> iotDeviceVos = iotDeviceService.listAndMapByUserId(Integer.valueOf(id));
        return Result.success(iotDeviceVos);
    }

    /**
     * 根据设备ID查询设备日志
     *
     * @param deviceId   设备ID
     * @param identified 设备标识
     * @param num        最近5条消息
     * @return 设备Things集合
     */
    @Override
    @GetMapping("/things")
    public Result listDeviceThings(@RequestParam(value = "deviceId") String deviceId,
                                   @RequestParam(value = "identified") String identified,
                                   @RequestParam(value = "num", defaultValue = "5") Integer num) {
        List<IotThings> iotThingsList = iotThingsService.listThingsByCondition(deviceId, identified, num);
        return Result.success(iotThingsList);
    }

}
